<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      :title="isUpdate ? t('common.edit') : t('common.create')"
      @register="register"
      @ok="handleSubmit"
      :destroyOnClose="true"
      :minHeight="400"
      :width="600"
    >
      <BasicForm @register="registerForm" />
      <Divider>{{ t('common.tip') }}</Divider>
      <p>{{ t('routes.demo.partSetting.tip1') }}</p>
      <p>{{ t('routes.demo.partSetting.tip3') }}</p>
      <p>{{ t('routes.demo.partSetting.tip4') }}</p>
      <p>{{ t('routes.demo.partSetting.tip2') }}</p>
      <p>{{ t('routes.demo.partSetting.tip5') }}</p>
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Divider } from 'ant-design-vue';
  import { getPartCategoryForm } from './data';

  import { createPartClass, updatePartClass, getPartFeature } from '/@/api/demo/system';

  const { t } = useI18n();
  const emit = defineEmits(['success', 'register']);
  const { createMessage } = useMessage();
  const isUpdate = ref(true);
  const newIndex = ref(null);

  // 定义弹窗是否可见
  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({ confirmLoading: false });
    resetFields();
    isUpdate.value = !!data?.isUpdate;
    // 特征项提示信息
    getPartFeature().then((res) => {
      const { partList, firstFeature, secondFeature } = res;

      const getFeatureValue = (label) => partList.find((item) => item.label === label)?.value || '';

      updateSchema({ field: 'firstFeature', helpMessage: getFeatureValue(firstFeature) });
      updateSchema({ field: 'secondFeature', helpMessage: getFeatureValue(secondFeature) });
    });

    const updateParams = unref(isUpdate)
      ? {
          partId: data.record.partid,
          counterID: data.record.counterId,
          firstFeature: data.record.firstFeature,
          secondFeature: data.record.secondFeature,
          disIndex: data.record.verdictpriority,
          isDefault: data.record.defaultag === '1',
        }
      : { disIndex: (newIndex.value = data.newIndex) };

    setFieldsValue(updateParams);
  });

  const [registerForm, { validate, setFieldsValue, resetFields, updateSchema }] = useForm({
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
    schemas: getPartCategoryForm(isUpdate),
    showActionButtonGroup: false,
  });

  async function handleSubmit() {
    const values = await validate();
    values.isDefault = values.isDefault ? '1' : '0';
    if (unref(isUpdate)) {
      // 更新
      try {
        setModalProps({ confirmLoading: true });
        await updatePartClass(values);
        closeModal();
        emit('success');
        createMessage.success(t('common.saveSuccess'));
      } finally {
        setModalProps({ confirmLoading: false });
      }
    } else {
      // 创建
      try {
        setModalProps({ confirmLoading: true });
        await createPartClass(values);
        closeModal();
        emit('success');
        createMessage.success(t('common.saveSuccess'));
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }
  }
</script>

<style lang="less" scoped></style>
